import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { notification } from 'ant-design-vue'
import 'ant-design-vue/es/notification/style/css'

const http: AxiosInstance = axios.create({
  // baseURL: 'http://localhost:3000', // 接口地址
  // baseURL: import.meta.env.VITE_PROXY_BASEURL,
  baseURL:'',//Mock
  timeout: 10000,
})

//请求拦截
http.interceptors.request.use(
  (config: AxiosRequestConfig) => {
    return Promise.resolve(config)
  },
  (error: any) => {
    return Promise.reject(error)
  },
)

// 响应拦截（根据接口数据格式进行调整）
http.interceptors.response.use(
  <T = any>(response: AxiosResponse): Promise<T> => {
    const { code, data, message }: HttpResponse = response.data;
    if (code === 0) {
      // 请求成功
      return data;
    }
    // 请求异常
    notification.error({
      message: code,
      description: message,
    })
    return Promise.reject(message)
  },
  // 请求失败
  (error: any) => {
    if (error.response) {
      const { data } = error.response;
      notification.error({
        message: '请求失败',
        description: data.message || error.message,
      })
    }
    return Promise.reject(error)
  }
)

//封装GET请求
export function get<T>(url: string, params = {}, config: AxiosRequestConfig = {}): Promise<T> {
  return http.get(url, {
    params,
    ...config
  })
}

//封装POST请求
export function post<T>(url: string, data = {}, config: AxiosRequestConfig = {}): Promise<T> {
  return http.post(url, data, config);
}

export default http